<template>
  <div class="password">
    <div class="password-msg">
      <p class="password-msg-welconme">欢迎登录</p>
      <div class="password-msg-line"></div>
      <div class="password-msg-title">
        <img src="../assets/img/register/logo_title.png" alt="" />
      </div>
    </div>
    <!-- 找回密码窗口 -->
    <div class="password-form">
      <div class="password-form-con">
        <div class="password-form-con-title">找回密码</div>
        <!-- 单位找回密码 -->
        <div class="password-form-con-company" v-if="activeName === 'company'">
          <el-form
            size="small"
            :model="formCompany.form"
            :rules="formCompany.rules"
            ref="formCompanyFrom"
            label-width="75px"
            style="margin-top: 24px; padding-bottom: 15px"
          >
            <el-form-item label="单位代码" prop="unit_code">
              <el-input v-model.trim="formCompany.form.unit_code" placeholder="请输入单位代码" maxLength="40" clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="申请人" prop="applyNmae">
              <el-input
                v-model.trim="formCompany.form.applyNmae"
                placeholder="请输入申请人姓名"
                maxLength="40"
                clearable
              >
              </el-input>
            </el-form-item>
            <el-form-item label="申请电话" prop="applyPhone">
              <el-input
                v-model.trim="formCompany.form.applyPhone"
                placeholder="请输入申请电话"
                maxLength="40"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <!-- 个人找回密码 -->
        <div class="password-form-con-personal" v-if="activeName === 'personal'">
          <el-form
            size="small"
            :model="formPersonal.form"
            :rules="formPersonal.rules"
            ref="formPersonalFrom"
            label-width="65px"
            style="padding-bottom: 15px"
          >
            <el-form-item label="账号" prop="mobile">
              <el-input v-model.trim="formPersonal.form.mobile" placeholder="请输入手机号" maxLength="40" clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input
                v-model.trim="formPersonal.form.password"
                placeholder="请输入新密码"
                type="password"
                maxLength="40"
                clearable
              >
              </el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="rePassword">
              <el-input
                v-model.trim="formPersonal.form.rePassword"
                placeholder="请再次输入密码"
                type="password"
                maxLength="40"
                clearable
              >
              </el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="sms_code">
              <div class="password-form-con-personal-code">
                <el-input
                  v-model.trim="formPersonal.form.sms_code"
                  placeholder="请输入验证码"
                  maxLength="40"
                  class="password-form-con-personal-code-input"
                />
                <el-button @click="getCode" :disabled="!codeFlag">
                  <span v-show="!codeFlag">{{ `${time}秒后重新获取` }}</span>
                  <span v-show="codeFlag">获取验证码</span>
                </el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!--按钮 -->
      <div class="password-form-btn">
        <div class="password-form-btn-apply" @click="clickSure">确定</div>
        <div class="password-form-btn-login" @click="clickLogin">去登录</div>
      </div>
      <!-- 底部 -->
      <div>
        <div class="password-form-con-line"></div>
        <div class="password-form-con-tips">
          <p @click="clickGuide">如何进行单位注册？</p>
          <p style="width: 1px; height: 10px; background: #d8d8d8; margin: 15px"></p>
          <div class="password-form-con-tips-query" @click="clickQuery">
            <span>申请进度查询</span>
            <div class="password-form-con-tips-query-serach" v-show="queryShow">
              <p @click="queryCompany">单位注册申请进度查询</p>
              <p @click="queryPassword">单位找回密码申请进度查询</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 引导页弹窗 -->
    <comGuideDialog v-if="queryVisible" :examineVisible="queryVisible" @closeDialog="clickGuide"></comGuideDialog>
    <!-- 注册进度查询弹窗 -->
    <comExamineDialo v-if="registerQuery" :queryOpen="registerQuery" @closeDialog="queryCompany"></comExamineDialo>
    <!-- 密码进度查询弹窗 -->
    <comComanyPassword v-if="passwordQuery" :queryOpen="passwordQuery" @closeDialog="queryPassword"></comComanyPassword>
    <!-- 重置完成弹窗 -->
    <el-dialog v-model="dialogVisible" width="25%">
      <div class="dialog-con">
        <div class="dialog-con-img">
          <img src="@/assets/img/register/icon_success.png" alt="" />
        </div>
        <div class="dialog-con-title">{{ dialogTitle }}</div>
        <el-form size="small" :model="formCompany" label-width="75px" style="margin-top: 24px">
          <el-form-item label="单位代码">
            <el-input v-model.trim="formCompany.companyNmae" maxLength="40"> </el-input>
          </el-form-item>
          <el-form-item label="初始密码">
            <el-input v-model.trim="formCompany.applyNmae" maxLength="40"> </el-input>
          </el-form-item>
        </el-form>
        <div class="dialog-con-btn" @click="clickLogin">去登录</div>
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import {ref, reactive, onMounted} from 'vue';
import {useRouter, useRoute} from 'vue-router';
import {ElMessage} from 'element-plus';
import comGuideDialog from '@/components/comGuideDialog/index.vue';
import comComanyPassword from '@/components/comComanyPassword/index.vue';
import comExamineDialo from '@/components/comExamineDialo/index.vue';
import registerApi from '@/api/register.ts';

const activeName = ref('personal');
// 单位
const formCompany = reactive({
  form: {unit_code: '', applyNmae: '', applyPhone: ''},
  rules: {
    unit_code: [{required: true, message: '请输入单位代码', trigger: 'blur'}],
    applyNmae: [{required: true, message: '请输入申请人姓名', trigger: 'blur'}],
    applyPhone: [{required: true, message: '请输入申请电话', trigger: 'blur'}],
  },
});
// 个人
const formPersonal = reactive({
  form: {mobile: '', password: '', rePassword: '', sms_code: ''},
  rules: {
    mobile: [{required: true, message: '请输入手机号', trigger: 'blur'}],
    password: [{required: true, message: '请输入新密码', trigger: 'blur'}],
    rePassword: [{required: true, message: '请再次输入密码', trigger: 'blur'}],
    sms_code: [{required: true, message: '请输入验证码', trigger: 'blur'}],
  },
});
const formCompanyFrom = ref(null); // 单位注册校验
const formPersonalFrom = ref(null); // 个人注册校验
const queryVisible = ref(false); // 引导页弹窗
const registerQuery = ref(false); // 单位注册进度弹窗
const passwordQuery = ref(false); // 忘记密码进度弹窗
const dialogVisible = ref(false);
const dialogTitle = ref('');
const codeFlag = ref(true);
const timer = ref(null);
const time = ref(0);
const queryShow = ref(false);

const route = useRoute();
const router = useRouter();

// 查询进度
const clickQuery = () => {
  queryShow.value = !queryShow.value;
};
// 引导页弹窗
const clickGuide = () => {
  queryVisible.value = !queryVisible.value;
};
// 注册进度查询弹窗
const queryCompany = () => {
  registerQuery.value = !registerQuery.value;
};
// 密码进度弹窗
const queryPassword = () => {
  passwordQuery.value = !passwordQuery.value;
};
// 获取验证码
const getCode = () => {
  if (!formPersonal.form.mobile) {
    ElMessage.warning('请先输入手机号');
    return;
  }
  registerApi
    .getMsgCode({mobile: formPersonal.form.mobile, action: 'reset_password '})
    .then((res) => {
      ElMessage.success(res.message);
      timeCountdown();
    })
    .catch((err) => {
      throw err;
    });
};
// 定时器
const timeCountdown = () => {
  const TIME_COUNT = 60;
  if (!timer.value) {
    time.value = TIME_COUNT;
    codeFlag.value = false;
    timer.value = setInterval(() => {
      if (time.value > 0 && time.value <= TIME_COUNT) {
        time.value--;
      } else {
        codeFlag.value = true;
        clearInterval(timer.value);
        timer.value = null;
      }
    }, 1000);
  }
};
// 确定
const clickSure = () => {
  let params;
  if (activeName.value === 'personal') {
    formPersonalFrom.value.validate((valid) => {
      if (valid) {
        if (formPersonal.form.password !== formPersonal.form.rePassword) {
          ElMessage.error('两次输入的密码不一致！');
          return;
        }
        params = {
          mobile: formPersonal.form.mobile,
          password: formPersonal.form.password,
          sms_code: formPersonal.form.sms_code,
        };
        registerApi
          .personalForgetPassword(params)
          .then((res) => {
            if (res.status === 0) {
              ElMessage.success(res.message);
              router.push({
                name: 'login',
                params: {
                  activeName: 'personal',
                  mobile: formPersonal.form.mobile,
                },
              });
            }
          })
          .catch((err) => {
            throw err;
          });
      }
    });
  } else {
    formCompanyFrom.value.validate((valid) => {
      if (valid) {
        params = {
          unit_code: formCompany.form.unit_code,
          apply_user: formCompany.form.applyNmae,
          apply_mobile: formCompany.form.applyPhone,
        };
        registerApi
          .companyForgetPassword(params)
          .then((res) => {
            if (res.status === 0) {
              ElMessage.success(res.message);
              router.push({
                name: 'login',
                params: {
                  activeName: 'company',
                },
              });
            }
          })
          .catch((err) => {
            throw err;
          });
      }
    });
  }
};
// 去登录
const clickLogin = () => {
  router.push({name: 'login', params: {activeName: activeName.value}});
};
onMounted(() => {
  if (route.params.activeName) {
    activeName.value = route.params.activeName;
  } else {
    activeName.value = 'personal';
  }
});
</script>

<style lang="scss" scoped>
.password {
  width: 100%;
  height: 100%;
  background: url(@/assets/img/login/login_bg.png);
  background-repeat: no-repeat;
  background-size: 100%;
  background-position: center center;
  position: relative;
  &-msg {
    position: absolute;
    top: 20.3%;
    left: 12.76%;
    color: #8fb4fe;
    width: 576px;
    overflow: hidden;
    &-welconme {
      font-size: 30px;
      color: #ffffff;
      font-weight: 600;
    }
    &-line {
      width: 248px;
      height: 2px;
      background: #d8d8d8;
    }
    &-title {
      width: 576px;
      margin: 31px 0 41px 0;
      font-size: 48px;
      color: #ffffff;
      > img {
        width: 100%;
      }
    }
  }
  &-form {
    position: absolute;
    top: 21.67%;
    right: 13.28%;
    width: 522px;
    background: #ffffff;
    border-radius: 10px;
    margin-left: 10.83%;
    padding: 50px 0;
    &-con {
      overflow: hidden;
      &-tabs {
        display: flex;
        justify-content: center;
      }
      &:deep(.el-tabs__nav-wrap) {
        &::after {
          position: static !important;
        }
      }
      &:deep(.el-tabs__item) {
        font-size: 22px;
        color: #999999;
      }
      &:deep(.is-active) {
        color: #333333;
        font-weight: bold;
        font-size: 22px;
      }
      &:deep(.el-tabs__active-bar) {
        width: 40px !important;
        height: 4px;
        position: absolute;
        left: 24px;
        background: #1182fb;
        border-radius: 6px;
      }
      .el-form {
        padding-left: 70px;
      }
      .el-form-item {
        margin-top: 25px;
      }

      &:deep(.el-input__wrapper) {
        background: #f2f4f9;
      }
      &:deep(.el-form-item__label) {
        text-align: left !important;
      }
      &:deep(.el-input__wrapper) {
        box-shadow: none !important;
      }
      &:deep(.el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label:before) {
        content: '' !important;
      }
      &-company {
        .el-input {
          width: 292px;
          height: 36px;
        }
      }
      &-personal {
        .el-input {
          width: 303px;
          height: 36px;
        }
        &-code {
          width: 303px;
          height: 36px;
          background: #f2f4f9;
          border-radius: 4px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .el-button {
            border: 0;
            margin: 0;
            color: #3369f4;
            font-size: 13px;
            background: #f2f4f9;
          }
        }
      }
      &-title {
        color: #333333;
        font-size: 22px;
        text-align: center;
        font-weight: 500;
        margin-top: 17px;
      }

      &-line {
        width: 383px;
        border: 1px solid #dcdcdc;
        margin: 0 auto;
        margin-top: 35px;
      }

      &-tips {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 25px;
        > p {
          cursor: pointer;
          color: #3369f4;
        }
        > span {
          margin: 0 19px;
        }
        &-query {
          position: relative;
          cursor: pointer;
          color: #3369f4;
          &-serach {
            > p {
              margin: 0;
              padding: 5px 16px;
              &:hover {
                background: rgba(0, 0, 0, 0.04);
              }
            }
            position: absolute;
            top: 0;
            left: 110px;
            color: #333;
            width: 230px;
            background: #ffffff;
            box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.06), 0px 2px 6px 6px rgba(0, 0, 0, 0.02),
              0px -2px 4px 0px rgba(0, 0, 0, 0.02);
          }
        }
      }
    }
    &-agreement {
      display: flex;
      padding: 0 70px;
      margin-top: 10px;
      > p {
        margin: 0;
        margin-left: 5px;
      }
    }
    &-btn {
      display: flex;
      flex-direction: column;
      margin-top: 25px;
      &-apply,
      &-login {
        width: 382px;
        height: 40px;
        margin: 0 auto;
        border-radius: 4px;
        color: #333333;
        border: 0;
        text-align: center;
        cursor: pointer;
        line-height: 40px;
      }
      &-apply {
        background: #3369f4;
        color: #fff;
      }
    }
  }
  .dialog-con {
    padding: 0 70px 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    &-img {
      width: 87px;
      height: 87px;
      margin: 0 auto;
      > img {
        width: 100%;
        height: 100%;
      }
    }
    &-title {
      color: #333333;
      font-size: 22px;
      text-align: center;
      font-weight: 500;
      margin-top: 17px;
    }
    &-tips {
      font-size: 13px;
      text-align: center;
      color: #333333;
      margin-top: 13px;
    }
    &-btn {
      width: 382px;
      height: 40px;
      background: #3369f4;
      border-radius: 4px;
      text-align: center;
      font-size: 14px;
      color: #ffffff;
      line-height: 40px;
      margin-top: 40px;
      cursor: pointer;
    }
    .el-form-item {
      margin-top: 25px;
    }
    &:deep(.el-input) {
      width: 289px;
    }
    &:deep(.el-form-item__label) {
      font-size: 13px;
      color: #333333;
      text-align: left !important;
    }
    &:deep(.el-input__wrapper) {
      width: 315px;
      height: 36px;
      background: #f2f4f9;
      border-radius: 4px;
      box-shadow: none !important;
    }
  }
}
</style>
